package org.example.顺序消息.生产;

import com.alibaba.fastjson.JSONArray;
import lombok.SneakyThrows;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.example.bean.Order;

/**
 * @Auther:Liu
 * @Testname:顺序局部消息发送
 * @Date:2025/8/22 16:49
 */
public class 顺序局部消息发送 {
    @SneakyThrows
    public static void main(String[] args) {
        DefaultMQProducer producer = new DefaultMQProducer("producer_group_a");
        producer.setNamesrvAddr("192.168.10.88:9876");
        producer.start();

        for (int i = 1; i < 21; i++) {

            Order order = new Order();
            order.setId(i);
            order.setPrice(Math.round(Math.random() * 100));
            int status = (int) (Math.random() * 4);
            order.setStatus(status);
            order.setName("订单" + i);
            System.out.println(order);
            String topic = "topic_a";
            String tags = "jubu";
            String body = JSONArray.toJSONString(order);
            Message message = new Message(topic, tags, body.getBytes());
            producer.send(message, (list, message1, o) -> list.get(status), status);

        }
        producer.shutdown();
    }
}
